<?php

class McCube_Html_Form_Element_MultiFields extends McCube_Html_Form_Abstract{

    protected $_valueKey   = '';
    protected $_contentKey = '';
    protected $_options    = array();
    protected $_fields     = array();

    public function __construct($name, $config=array()){
        parent:: __construct($name, $config);
         $this->id = str_replace(array('[',']') ,array('_',''), $name);

        if(isset($config['label']))
            $this->label = $config['label'];

        foreach($config['fields'] as $fieldname=>$config){
             $this->_fields[$fieldname] = $this->field($fieldname);
        }

        $this->_fields['button'] = new McCube_Html_Form_Element_Input('button', '');
        $this->_fields['button']->setValue('Ajouter');
        $this->_fields['button']->setAttr('onclick'
            , '$.mcCube.form.element.multiText.addItem(\''.$this->id.'\',\''.$this->name.'\');'
        );
    }

    public function setModel($model){
        $this->_model = $model;
        foreach($this->_fieldsName as $fieldname)
            $this->field($fieldname)->setModel($model);

        return $this;
    }

    /** */
    public function __toString(){
        return $this->render();
    }

    /** */
    protected function _build(){
        return '<div id="'.$this->id.'_div">'
            .implode('', $this->_fields)

        .'</div>';
    }
}